package com.atguigu.springboot.controller;

import com.atguigu.springboot.pojo.Dog;
import com.atguigu.springboot.service.DogService;
import com.atguigu.springboot.utils.PageBean;
import com.atguigu.springboot.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Author  王东剑
 * Created on 26/3/2024 PM6:43
 * Package_name com.atguigu.springboot.controller
 * Description
 */
@RestController
@RequestMapping("dog")
@CrossOrigin
@Slf4j
public class DogController {
	@Autowired
	private DogService dogService;
	/**
	 * getDogList
	 * deleteDogById
	 * insertDog
	 * updateDog
	 */
	@GetMapping("getDogList")
	public R getDogList(int currentPage,int pageSize){
		PageBean<Dog> dogPageBean = dogService.getDogList(currentPage,pageSize);
		return R.ok(dogPageBean);
	}

	@GetMapping("deleteDogById")
	public R deleteDogById(int id){
		dogService.deleteDogById(id);
		return R.ok("删除成功");
	}

	@PostMapping("insertDog")
	public R insertDog(@RequestBody @Validated Dog dog, BindingResult result){
		if(result.hasErrors()){
			log.info("参数错误");
			return R.fail("参数错误");
		}
		log.info("dog = {}",dog);
		dogService.insertDog(dog);
		return R.ok("新增成功");
	}

	@PostMapping("updateDog")
	public R updateDog(@RequestBody @Validated Dog dog, BindingResult result){
		if(result.hasErrors()){
			log.info("参数错误");
			return R.fail("参数错误");
		}
		log.info("dog = {}",dog);
		dogService.updateDog(dog);
		return R.ok("更新成功");
	}

	@GetMapping("getDogById")
	public R getDogById(int dogId){
		Dog dog = dogService.getDogById(dogId);
		return R.ok(dog);
	}
}
